//
//  ST_ChooseGenderController.swift
//  btvideo
//
//  Created by mac on 2020/9/27.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_ChooseGenderController: UIViewController,ST_RegisterRouteProtocol{
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //ST_BTGlobalDataCenter.shared.beginLogPageView("SelectGender")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        //ST_BTGlobalDataCenter.shared.endLogPageView("SelectGender")
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.backgroundColor = .white
        view.addSubview(sex_info_btn)
        sex_info_btn.frame = CGRect.init(x: 150, y: 73-20+statusBarHeight, width: 153+10+30, height: 30)
        sex_info_btn.alpha = 0.1
        
        
        view.addSubview(sex_sub_label)
        sex_sub_label.frame = CGRect.init(x: 150, y: 106-20+statusBarHeight + 3, width: 153+10+30, height: 30)
        sex_sub_label.alpha = 0.1
        
        //        男生组
        view.addSubview(male_icon)
        male_icon.frame = CGRect.init(x: ScreenWidth, y: sex_sub_label.frame.maxY + 40, width: 100, height: 100)
        
        let sexRadioY = male_icon.frame.maxY + 20
        
        view.addSubview(male_radio_btn)
        male_radio_btn.frame = CGRect.init(x: ScreenWidth, y: sexRadioY, width: 24, height: 24)
        
        view.addSubview(male_label)
        male_label.frame = CGRect.init(x: ScreenWidth, y: sexRadioY, width: 45, height: 28)
        
        //        女生组
        view.addSubview(female_icon)
        female_icon.frame = CGRect.init(x: ScreenWidth, y: sex_sub_label.frame.maxY + 40, width: 100, height: 100)
        
        view.addSubview(female_radio_btn)
        female_radio_btn.frame = CGRect.init(x: ScreenWidth, y: sexRadioY, width: 24, height: 24)
        
        view.addSubview(female_label)
        female_label.frame = CGRect.init(x: ScreenWidth, y: sexRadioY, width: 45, height: 28)
        
        //        点击范围
        view.addSubview(male_click_btn)
        //        male_click_btn.frame = CGRect.init(x: 70, y: male_icon.y, width: 20+66, height: 24+66+10)
        male_click_btn.snp.makeConstraints { (make) in
            make.left.top.equalTo(male_icon)
            make.right.bottom.equalTo(male_label)
        }
        
        
        view.addSubview(female_click_btn)
        female_click_btn.snp.makeConstraints { (make) in
            make.left.top.equalTo(female_icon)
            make.right.bottom.equalTo(female_label)
        }
        
        //TianRegisterStepManager.shared.addHelperBtn(root_view: view)
    }
    
    private var firstAnimation = false
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        if firstAnimation{
            return
        }
        
        firstAnimation = true
        
        UIView.zenly_animate(withDelay: 0.25, animations: {
            
            self.sex_info_btn.x = -6
            
            self.sex_info_btn.alpha = 1
            
            self.sex_sub_label.x = 30
            
            self.sex_sub_label.alpha = 1
            
        }) { (f) in
            
        }
        
        UIView.zenly_animate(withDelay: 0.35, animations: {
            
            //        男生组
            
            self.male_radio_btn.x = self.male_icon_min_x
            
            self.male_label.x = self.male_icon_min_x + 31
            
            self.male_icon.x = self.male_icon_min_x - 13
            
            //        女生组
            let offset:CGFloat = ScreenWidth - 87/375.0*ScreenWidth - 80
            
            self.female_radio_btn.x = offset
            
            self.female_label.x = offset + 24 + 10
            
            self.female_icon.x = offset - 13
            
        }) { (f) in
            
            self.view.addSubview(self.next_btn)
            
            self.next_btn.frame = CGRect.init(x:  ScreenWidth - 45 - 40, y: self.female_label.frame.maxY + 60, width: 45, height: 45)
            
            self.next_btn.alpha = 0.1
            
            UIView.zenly_animate(animations: {
                
                self.next_btn.alpha = 1
                
            }) { (f) in
                
            }
        }
        
        
        
        
    }
    
    //    MARK: - Properties
    lazy var sex_info_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("你的性别是？")
        control.setTitleColor(UIColor.init(hexString: "#333333")!, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.semibold)
        control.setImageForAllStates(ST_ImageUtils.imageName("register_icon_emoji性别")!)
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)
        //        control.backgroundColor = Color.random
        return control
    }()
    
    
    fileprivate lazy var sex_sub_label:UILabel = {
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "注册后，性别无法修改哦")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold),.foregroundColor: UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        return label
    }()
    
    lazy var male_radio_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("mycenter_icon_未选择")!)
        control.setImage(ST_ImageUtils.imageName("mycenter_icon_选择"), for: UIControl.State.selected)
        return control
    }()
    
    lazy var female_radio_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("mycenter_icon_未选择")!)
        control.setImage(ST_ImageUtils.imageName("mycenter_icon_选择"), for: UIControl.State.selected)
        return control
    }()
    
    fileprivate lazy var male_icon_min_x = 70/375.0*ScreenWidth
    
    lazy var male_icon:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImage(ST_ImageUtils.imageName("ChooseGender_male_icon"), for: UIControl.State.normal)
        control.setImage(ST_ImageUtils.imageName("ChooseGender_male_icon_sel"), for: UIControl.State.selected)
        control.isUserInteractionEnabled = false
        return control
    }()
    
    
    lazy var female_icon:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImage(ST_ImageUtils.imageName("ChooseGender_female_icon"), for: UIControl.State.normal)
        control.setImage(ST_ImageUtils.imageName("ChooseGender_female_icon_sel"), for: UIControl.State.selected)
        control.isUserInteractionEnabled = false
        return control
    }()
    
    lazy var male_label:UIButton =  {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("男生")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#999999")!)
        control.setTitleColor(UIColor.init(hexString: "#333333")!, for: UIControl.State.selected)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()
    
    lazy var female_label:UIButton =  {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("女生")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#999999")!)
        control.setTitleColor(UIColor.init(hexString: "#333333")!, for: UIControl.State.selected)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    lazy var female_click_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
//                control.backgroundColor = UIColor.init(white: 0.1, alpha: 0.4)
        control.addTarget(self, action: #selector(selected_sex(sender:)), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    lazy var male_click_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
//                control.backgroundColor = UIColor.init(white: 0.1, alpha: 0.4)
        control.addTarget(self, action: #selector(selected_sex(sender:)), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    fileprivate var last_selected_sex_btn:UIButton?
    
    var sex = ST_SEX.unkown
    // 头像👤
    lazy var avatar_info_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("你的头像呢？")
        control.setTitleColor(UIColor.init(hexString: "#333333")!, for: UIControl.State.normal)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.semibold)
        control.setImageForAllStates(ST_ImageUtils.imageName("register_icon_emoji头像")!)
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 0)
        //        control.backgroundColor = Color.random
        return control
    }()
    
    
    lazy var next_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setImageForAllStates(ST_ImageUtils.imageName("register_bot_下一步")!)
        
        control.setImage(ST_ImageUtils.imageName("register_bot_下一步diss"), for: UIControl.State.disabled)
        
        control.addTarget(self, action: #selector(next_action), for: UIControl.Event.touchUpInside)
        
        control.isEnabled = false
        
        control.alpha = 0
        
        return control
    }()
    
}


extension ST_ChooseGenderController{
    
    @objc func selected_sex(sender:UIButton){
        
        next_btn.isEnabled = true
        
        if sender == female_click_btn{
            female_icon.isSelected = true
            female_label.isSelected = true
            female_radio_btn.isSelected = true
            
            male_icon.isSelected = false
            male_label.isSelected = false
            male_radio_btn.isSelected = false
            
            sex = ST_SEX.female
            
        }else{
            
            sex = ST_SEX.male
            
            female_icon.isSelected = false
            female_label.isSelected = false
            female_radio_btn.isSelected = false
            
            
            male_icon.isSelected = true
            male_label.isSelected = true
            male_radio_btn.isSelected = true
            
        }
        
        last_selected_sex_btn = sender
    }
    
    
    
    
    @objc func next_action(){
        
        PlayFeedBack()
        
        
        let attrString = NSMutableAttributedString(string: "提示")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        let messageAttrString = NSMutableAttributedString(string: "性别选定后无法修改")
        let attr2: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.38, green: 0.4, blue: 0.44, alpha: 1)]
        messageAttrString.addAttributes(attr2, range: NSRange(location: 0, length: messageAttrString.length))
        
        
        
        let alert = ST_BTAlertViewController.init(title: attrString, message: messageAttrString, imageName: "首页_icon_提示")
        
        alert.overCurrentContext = true
        
        alert.over_current_context_alpha = "0.4"
        
        alert.confirm_closure = {
            
            self.save_sex_handle()
        }
        
        GlobalPresent(toController: alert, animated: false)
        
    }
    
    fileprivate func save_sex_handle(){
        
        
        self.pushNextRegisterController(parameters: ["gender" : self.sex.rawValue])
        
    }
    
}
